﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Business.Models;
using System.Data;

namespace Business.DAO
{
    public class MonHocDAO
    {
        private ISSContext db = new ISSContext();
        public List<MonHoc> GetAll()
        {
            return db.MonHoc.ToList();
        }

        public MonHoc Get(int id)
        {
            MonHoc MonHoc = db.MonHoc.Find(id);
            return MonHoc;
        }

        public IEnumerable<MonHoc> GetByTruongHoc(int idTruongHoc)
        {
            try
            {
                return db.MonHoc.Where(m => m.KhoiLop.IdTruongHoc == idTruongHoc).ToList<MonHoc>();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public int Create(MonHoc MonHoc)
        {
            try
            {
                db.MonHoc.Add(MonHoc);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Edit(MonHoc MonHoc)
        {
            try
            {
                db.Entry(MonHoc).State = EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Delete(int id)
        {
            try
            {
                MonHoc MonHoc = db.MonHoc.Find(id);
                db.MonHoc.Remove(MonHoc);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public List<MonHoc> GetByKhoiLop(KhoiLop KhoiLop)
        {
            var query = from monhoc in db.MonHoc where monhoc.KhoiLop == KhoiLop select monhoc;
            List<MonHoc> lstMonHoc = query.ToList<MonHoc>();
            return lstMonHoc;
        }

        public List<MonHoc> GetByQuanTri(QuanTri QuanTri)
        {
            var query = from monhoc in db.MonHoc where monhoc.QuanTri == QuanTri select monhoc;
            List<MonHoc> lstMonHoc = query.ToList<MonHoc>();
            return lstMonHoc;
        }

        public List<MonHoc> GetByKhoiLop_QuanTri(KhoiLop KhoiLop, QuanTri QuanTri)
        {
            var query = from monhoc in db.MonHoc where monhoc.QuanTri == QuanTri && monhoc.KhoiLop == KhoiLop select monhoc;
            List<MonHoc> lstMonHoc = query.ToList<MonHoc>();
            return lstMonHoc;
        }
    }
}